package com.xuexiang.xuidemo.page.base;

import ohos.agp.components.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 单文字的PageList
 *
 * @since 2021-03-31
 */
public abstract class PageClassListAbilitySlice extends SimpleTextListAbilitySlice<PageClassListAbilitySlice.PageClassText> {
    @Override
    protected List<PageClassListAbilitySlice.PageClassText> getDatas() {
        return getPages().stream().map(PageClassListAbilitySlice.PageClassText::new).collect(Collectors.toList());
    }

    /**
     * 列表的 AbilitySliceClass, 对象必须继承自BasePageAbilitySlice且设置@Page注解
     *
     * @return List Class
     */
    protected abstract List<Class<? extends BasePageAbilitySlice>> getPages();

    @Override
    public void onItemClick(Component component, int position, PageClassListAbilitySlice.PageClassText data) {
        PageUtils.jump(getAbility(), data.pageClz);
    }

    /**
     * PageClass转SimpleText内部类
     *
     * @since 2021-03-31
     */
    public static class PageClassText implements SimpleTextListAbilitySlice.SimpleText {
        private final Class<? extends BasePageAbilitySlice> pageClz;
        private final Page page;

        private PageClassText(Class<? extends BasePageAbilitySlice> pageClz) {
            this.pageClz = pageClz;
            page = PageUtils.getPage(pageClz);
        }

        @Override
        public String getName() {
            return page.name();
        }

        @Override
        public String getDescribe() {
            return page.subTitle();
        }
    }
}
